"""
usage: python run_all_test [filename]

args:
    filename - prefix (before _test.py) of file to execute [default '*']

return codes:
    0: all is OK
    1: some tests fails
"""
import gettext
import os
import sys
import unittest
import shutil
from kivy.lang import Builder

IND = ' ' * 4


def indent(s, ind_count):
    return IND * ind_count + s.replace('\n', '\n' + IND * ind_count)

PATH = os.path.abspath('.')
LOCALE_PATH = os.path.join(PATH, 'locales')
shutil.copyfile(os.path.join(PATH, 'data', 'config.ini.template'),
                os.path.join(PATH, 'data', 'config.ini'))
gettext.bindtextdomain('thehat', LOCALE_PATH)
lang = gettext.translation('thehat', LOCALE_PATH, ['ru_RU'],
                           codeset='utf-8')
lang.install(True)
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/start_game.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/add_player.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/add_words.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/players_order.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/from_to.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/gameconfig.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/countdown_screen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/results_of_one_pair.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/guesswordscreen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/gameresults.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/results_general.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/edit_players.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/loadgame.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/curresults.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/quick_game_settings.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'ui/welcome_screen.kv'))
Builder.load_file(os.path.join(os.path.dirname(__file__),
                               'mainview.kv'))
if len(sys.argv) > 1:
    patt = sys.argv[1]
else:
    patt = '*'
tests = unittest.TestLoader().discover('.', pattern='{}_test.py'.format(patt))
result = unittest.TestResult()
result.buffer = True
tests.run(result)
print '\n' * 30
if len(result.errors) > 0:
    print '-'*5, 'Errors', '-'*5
for el in result.errors:
    print el[1]
if len(result.failures) > 0:
    print '-'*5, 'Failures', '-'*5
for el in result.failures:
    print el[1]
print '\n'
print 'Total discovered {} test cases for {}'.format(tests.countTestCases(),
                                                     patt)
print 'Total {} failures'.format(len(result.failures))
print 'Total {} errors'.format(len(result.errors))
print 'Total {} tests ran'.format(result.testsRun)
if len(result.errors)+len(result.failures) > 0:
    sys.exit(1)
print
print 'OK'
